package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.feign.CacheService;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.service.StrategyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author lenovo
 * @version V1.0
 * @Date 2021/11/22 21:49
 */
@Service
@Slf4j
public class FilterChainServiceImpl implements FilterChainService {

    @Autowired
    Map<String, StrategyService> strategyServiceMap;

    @Autowired
    private CacheService cacheService;

    @Override
    public void executerFilter(StandardSubmit standardSubmit) {

        //1.调用cache模块获取过滤器的执行顺序
        Map<String, String> map = cacheService.hMGet(CacheConstants.CACHE_PREFIX_CLIENT + standardSubmit.getClientID());

        //2.根据逗号分隔，获取过滤器执行顺序数组
        String filters = map.get("filters");
        String[] filtersArray = filters.split(",");

        //3.遍历过滤器执行顺序数组
        for (String filter : filtersArray) {
            //4.获取执行的过滤器的实现类
            StrategyService strategyService = strategyServiceMap.get(filter);

            //5.执行过滤方法
            Boolean flag = strategyService.execute(standardSubmit);

            //6.判断执行结果是否放行
            if (!flag){
                log.info("【策略模块 - 策略管理者】，执行策略失败，策略名称={}",filter);
                break;
            }
        }

    }
}
